{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "62591027-7f4b-4292-83a6-d6b76fae6099",
   "metadata": {},
   "source": [
    "# 第四节、ndarray的数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14efb891-494b-4054-b6f3-87cc1df42245",
   "metadata": {},
   "source": [
    "常见的ndarray数据类型举例：\n",
    "\n",
    "| 数据类型 | 描述 | 示例 |  \n",
    "| --- | --- | --- |  \n",
    "| `int8`, `int16`, `int32`, `int64` | 整数类型，位数表示存储大小 | `[[1, 2], [3, 4]]`|  \n",
    "| `uint8`, `uint16`, `uint32`, `uint64` | 无符号整数类型 | `[[1, 2], [3, 4]]` (|  \n",
    "| `float16`, `float32`, `float64` | 浮点数类型 | `[[1.1, 2.2], [3.3, 4.4]]`  |  \n",
    "| `complex64`, `complex128` | 复数类型 | `[[1+2j, 3+4j], [5+6j, 7+8j]]`  |  \n",
    "| `bool` | 布尔类型 | `[[True, False], [False, True]]` |  \n",
    "| `object` | Python对象类型（如字符串或混合类型） | `[['a', 'b'], [1, 2.0]]` |  \n",
    "| `string_` (固定长度字符串) | 固定长度的字符串类型（如`U5`表示长度为5的Unicode字符串） | `[['hello'], ['world']]`  |  \n",
    "| `timedelta64` | 存储时间间隔 | `numpy.array([numpy.timedelta64(1, 'D'), numpy.timedelta64(2, 'D')])` |  \n",
    "| `datetime64` | 存储日期和时间 | `numpy.array([numpy.datetime64('2023-01-01'), numpy.datetime64('2023-01-02')])` |  \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d68581a0-e6fd-4b60-99be-35dee3f9191b",
   "metadata": {},
   "source": [
    "## 创建ndarray数组的时候指定数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a45f5d60-ece2-40c7-b67d-91d3670e01d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c15ea35f-4da2-48d3-85c2-a9a4a6376dba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4.], dtype=float32)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = np.array([1, 2, 3, 4], dtype=np.float32)\n",
    "n1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd6e4b40-218f-4904-8df2-f6c433548b72",
   "metadata": {},
   "source": [
    "## 通过astype方法来更改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "340ca8fc-2c65-4752-847e-b376b0c8fa88",
   "metadata": {},
   "outputs": [],
   "source": [
    "n1 = n1.astype(np.int32)   # astype方法会返回一个新的数组，如果你想替换掉原来的可以重新赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "edb3857c-6cd1-45d2-ba0f-b761fc0aa645",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2f7330fe-9629-400b-bbfe-294e3b23fbc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17f5f90e-78dc-46df-a452-772f7fbe7434",
   "metadata": {},
   "source": [
    "## 通过asarray来重新创建数组并且指定数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "da7977a8-ee37-4a2f-9df6-807e70d67b6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4], dtype=uint8)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 = np.asarray(n1, dtype=np.uint8)\n",
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72cd910a-438c-46bd-9411-0b34d5e8c00d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
